package vsvm.graphics.charts;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.commons.math.MathException;

import vsvm.classifier.AbstractTestSet;
import vsvm.classifier.TestCase;
import vsvm.data.model.AbstractAttribute;
import vsvm.math.CoreMath;
import vsvm.math.statistics.Characteristics;
import vsvm.math.statistics.distributions.NormalDistribution;
import vsvm.math.statistics.histogram.Histogram;
import vsvm.math.statistics.histogram.HistogramItem;

/**
 * 
 *	Chart for probability distributions of
 *  actual Postivive and Negative classes
 *  
 */
public class PNROCChart extends AbstractChart{
	private static final long serialVersionUID = 100L;

	private AbstractTestSet testSet;
	public PNROCChart(String title, AbstractTestSet testSet) {
		super(title);		
		this.testSet = testSet;
	}
	
	protected Graphics2D drawChart(Graphics2D g) {
		// TODO Auto-generated method stub
		
		return null;
	}
	
	protected void prepareColors() {
		this.setColors(new Color[]{Color.BLACK, Color.BLUE, Color.RED});
	}
	
	protected void prepareChart() {
		double positive = this.testSet.getPositiveClass();
		double negative = this.testSet.getNegativeClass();
		int pcount = 0;
		int ncount = 0;
		
		ArrayList <Double> negatives = new ArrayList();
		ArrayList <Double> positives = new ArrayList();
		
		Iterator <TestCase> it = this.testSet.getTestCases();
		
		while (it.hasNext()) {
			TestCase tcase = it.next();
			if (Math.abs(tcase.getActualClass() - positive) < CoreMath.EPS) pcount++;
			else ncount++;				
		}
		
		double[] posArray = new double[pcount];
		double[] negArray = new double[ncount];
		int i = 0; int j = 0;
		
		it = this.testSet.getTestCases();
		while (it.hasNext()) {
			TestCase tcase = it.next();
			if (Math.abs(tcase.getActualClass() - positive) < CoreMath.EPS)
				posArray[i++] = tcase.getProbabilities()[1];
			else
				negArray[j++] = tcase.getProbabilities()[1];
		}
		
		/*Histogram posHist = new Histogram(posArray, AbstractAttribute.NUMERIC_TYPE);
		Histogram negHist = new Histogram(negArray, AbstractAttribute.NUMERIC_TYPE);
		
		int itemsCnt = posHist.getHistogramData().getItemsCount();
		for (int k = 0; k < itemsCnt; k++){			
			HistogramItem item = posHist.getHistogramData().getHistogramItem(k);
			this.addPoint(0, (item.getStart() + item.getEnd())/2, item.getFrequency(), true);			
		}
		
		itemsCnt = negHist.getHistogramData().getItemsCount();
		for (int k = 0; k < itemsCnt; k++){			
			HistogramItem item = negHist.getHistogramData().getHistogramItem(k);
			this.addPoint(1, (item.getStart() + item.getEnd())/2, item.getFrequency(), true);			
		}	
		
		double max = negHist.getMaxFrequency();
		if (max < posHist.getMaxFrequency()) max = posHist.getMaxFrequency(); 
		this.addPoint(2, 0.5, 0, true);
		this.addPoint(2, 0.5, max, true);*/
		
		NormalDistribution nd = new NormalDistribution(Characteristics.getMean(posArray), Characteristics.getStDev(posArray));
		
		int pointCnt = 100;
		double std  = nd.getStandardDeviation();
		double mean = nd.getMean();
		double start = mean - 4*std;
		double end   = mean + 4*std;
		double step  = 8*std/100;
		double max = Double.NEGATIVE_INFINITY;
		
		
		for (int k = 0; k < pointCnt; k++) {
			double d = nd.getDensity(start);
			max = d > max ? d : max;
			this.addPoint(0, start, d, true);
			start += step;
		}
		
		nd = new NormalDistribution(Characteristics.getMean(negArray), Characteristics.getStDev(negArray));		
		
		std  = nd.getStandardDeviation();
		mean = nd.getMean();
		start = mean - 4*std;
		end   = mean + 4*std;
		step  = 8*std/100;
			
		for (int k = 0; k < pointCnt; k++) {			
			double d = nd.getDensity(start);
			max = d > max ? d : max;
			this.addPoint(1, start, d, true);
			start += step;
		}
		
		this.addPoint(2, 0.5, 0, true);
		this.addPoint(2, 0.5, max, true);
		
		chartPrepared = true;
		
	}
}
